% This file computes the partial R^2 to test for weak instruments
% as proposed by Shea (1997)
%Tobias Pfutze December 2009

% Upload Data in order: pripart oppmaxpart remit_prophh_intl
% dist_border cycleB1 cycleB2 zona_norte labinc_perhh_log
% labinc_perhh_stdev illiterate indig margmuni00 poptot_2000 unempl
% raildistjuarez_05 histmig24_edoprop00 oppwin_since80 pripart1 oppmaxpart1

RAW=load('/Users/tpfutze/Dropbox/Research/Participation/Matlab/Data/participation_adjall.txt');
[N,M]=size(RAW);

% Replace total population by its logged value
RAW(:,13)=log(RAW(:,13));

% Create two separate matrices conditional on whether municipality has 
% had an opposition party in office since 1980
s0=1;
s1=1;
for i=1:N
    if (RAW(i,17)==0)
        RAW0(s0,1:M)=RAW(i,1:M);
        s0=s0+1;
    elseif RAW(i,17)==1
        RAW1(s1,1:M)=RAW(i,1:M);
        s1=s1+1;
    end
end
[N0,M0]=size(RAW0);
[N1,M1]=size(RAW1);


%%%%%%%%%%%%%%%%%%%%%%%
% Create all matrices %
%%%%%%%%%%%%%%%%%%%%%%%
% Endogenous independent variables as dependent variable
y=RAW(:,3);
y0=RAW0(:,3);
y1=RAW1(:,3);

% All independent Variables
x1=[ones(N,1) RAW(:,3:7)];                               
x2=[ones(N,1) RAW(:,3:14)];  

x01=[ones(N0,1) RAW0(:,3:7)];                               
x02=[ones(N0,1) RAW0(:,3:14)];  

x11=[ones(N1,1) RAW1(:,3:7)];                               
x12=[ones(N1,1) RAW1(:,3:14)];    

% All exogenous independent Variables
xx1=[ones(N,1) RAW(:,4:7)];                               
xx2=[ones(N,1) RAW(:,4:14)];  

xx01=[ones(N0,1) RAW0(:,4:7)];                               
xx02=[ones(N0,1) RAW0(:,4:14)];  

xx11=[ones(N1,1) RAW1(:,4:7)];                               
xx12=[ones(N1,1) RAW1(:,4:14)];   


% All instruments
z1=[ones(N,1) RAW(:,15:16) RAW(:,4:7)];                               
z2=[ones(N,1) RAW(:,15:16) RAW(:,4:14)]; 

z01=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:7)];                               
z02=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:14)];  

z11=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:7)];                               
z12=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:14)];  



%%%%%%%%%%%%%%%%%%%
% DEFINE MATRICES %
%%%%%%%%%%%%%%%%%%%

R2=zeros(1,6);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ENTIRE SAMPLE %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1ST CASE
casenum=1;
Y=y;
X=x1;
XX=xx1;
Z=z1;
[n,k1] = size(Y);
[n,k2] = size(XX);


[r2] = Partial_R2(Y, X, XX, Z, k1, k2);

R2(:,casenum)=r2;

clear Y X XX Z k1 k2 r2;

%2ND CASE
casenum=casenum+1;
Y=y;
X=x2;
XX=xx2;
Z=z2;
[n,k1] = size(Y);
[n,k2] = size(XX);

[r2] = Partial_R2(Y, X, XX, Z, k1, k2);

R2(:,casenum)=r2;

clear Y X XX Z k1 k2 r2;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%   ALWAYS PRI   %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1ST CASE
casenum=casenum+1;
Y=y0;
X=x01;
XX=xx01;
Z=z01;
[n,k1] = size(Y);
[n,k2] = size(XX);


[r2] = Partial_R2(Y, X, XX, Z, k1, k2);

R2(:,casenum)=r2;

clear Y X XX Z k1 k2 r2;

%2ND CASE
casenum=casenum+1;
Y=y0;
X=x02;
XX=xx02;
Z=z02;
[n,k1] = size(Y);
[n,k2] = size(XX);

[r2] = Partial_R2(Y, X, XX, Z, k1, k2);

R2(:,casenum)=r2;

clear Y X XX Z k1 k2 r2;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ALREADY OPPOSITION %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1ST CASE
casenum=casenum+1;
Y=y1;
X=x11;
XX=xx11;
Z=z11;
[n,k1] = size(Y);
[n,k2] = size(XX);


[r2] = Partial_R2(Y, X, XX, Z, k1, k2);

R2(:,casenum)=r2;

clear Y X XX Z k1 k2 r2;

%2ND CASE
casenum=casenum+1;
Y=y1;
X=x12;
XX=xx12;
Z=z12;
[n,k1] = size(Y);
[n,k2] = size(XX);

[r2] = Partial_R2(Y, X, XX, Z, k1, k2);

R2(:,casenum)=r2;

clear Y X XX Z k1 k2 r2;


save PartiesPartialR2_Results R2;
